package com.flynn.rock.crm.repository.customer;

import com.flynn.rock.crm.entity.customer.CustomerOrderEntity;
import com.flynn.rock.enums.customer.CustomerOrderStatus;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CustomerOrderRepository extends JpaRepository<CustomerOrderEntity, Long>,
        JpaSpecificationExecutor<CustomerOrderEntity> {

    boolean existsByCustomerIdAndOrderStatusIn(Long customerId, List<CustomerOrderStatus> status);

    @Transactional
    @Modifying
    @Query("update CustomerOrderEntity as t set t.orderStatus = :orderStatus where t.customerId = :customerId")
    void updateCustomerOrderStatus(@Param("customerId") Long customerId,
                                   @Param("orderStatus") CustomerOrderStatus orderStatus);
}
